package com.wedimob.slideshare.task.disk;

import java.net.URI;

import com.wedimob.slideshare.RuntimeConfig;
import com.wedimob.slideshare.bo.SharedDocument;
import com.wedimob.slideshare.bo.Summary;
import com.wedimob.slideshare.task.PoolExecutor;
import com.wedimob.slideshare.task.Task;
import com.wedimob.slideshare.task.TaskType;

public class TaskDocumentCBRWriter extends Task<SharedDocument, SharedDocument> {

    public TaskDocumentCBRWriter(PoolExecutor pool, SharedDocument input, int timeout) {
        super(TaskType.DISK, pool, input, 0, timeout);
    }

    static public URI getTmpPath(Summary summary) {
        return PathHelper.buildTmpPath(summary.getAuthor(), summary.getTitle());
    }

    static public URI getCbrPath(Summary summary) {
        try {
            return PathHelper.buildOutputFile(summary.getTitle().toString() + " - "
                    + summary.getAuthor().substring(0, summary.getAuthor().length() - 1)
                    + ".cbr");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void run() {
        try
        {
            RuntimeConfig.getInstance().getWriter().zip(getTmpPath(input.getSummary()), getCbrPath(input.getSummary()));
        } catch (Exception e)
        {
            e.printStackTrace();
            this.e = e;
        }
    }
}
